﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Web.Mvc;
using Kendo.Mvc;
using Kendo.Mvc.Infrastructure;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using SecurityApi.DataAccess;
using SecurityAdmin.Web.Models;

namespace SecurityAdmin.Web.Controllers
{
    public class MembershipController : BaseController
    {

        public ActionResult GetMembership(int userId)
        {
            SecurityApi.DataAccess.MembershipRepository repository = new MembershipRepository();
            CustomMembership membership =  repository.GetMembershipById(userId);
            if (null == membership)
            {
                return RedirectToAction("MembershipNotExists", new { userId = userId});
            }
            return View(membership);
        }

        public ActionResult MembershipNotExists(int userId)
        {
            SecurityApi.DataAccess.EnterpriseUserRepository repository = new SecurityApi.DataAccess.EnterpriseUserRepository();
            EnterpriseUser user = repository.GetUserById(userId);
            return View(user);
        }


        public ActionResult CreateMembership(int userId)
        {
            SecurityApi.DataAccess.EnterpriseUserRepository repository = new SecurityApi.DataAccess.EnterpriseUserRepository();
            EnterpriseUser user = repository.GetUserById(userId);

            RegisterModel registerModel = new RegisterModel();
            registerModel.UserId = userId;
            registerModel.Email = user.UserName;
            registerModel.UserName = user.UserName;

            return View(registerModel);
        }

        [HttpPost]
        public ActionResult CreateMembership(RegisterModel registerModel)
        {
            CustomMembership membership = new CustomMembership();
            membership.UserId = registerModel.UserId;
            membership.Email = registerModel.UserName;
            membership.PasswordQuestion = string.Empty;
            membership.PasswordAnswer = string.Empty;
            membership.PasswordFormat = 1;
            membership.IsApproved = true;
            membership.IsLockedOut = false;
            membership.CreateDate = DateTime.Now;
            membership.LastLoginDate = membership.CreateDate;
            membership.LastPasswordChangedDate = membership.CreateDate;
            membership.LastLockoutDate = membership.CreateDate;
            membership.FailedPasswordAttemptCount = 0;
            membership.FailedPasswordAttemptWindowStart = membership.CreateDate;
            
            return View(registerModel);
        }

    }
}
